<template>
  <div class="register">
    <el-form
      :model="registerUser"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="registerForm"
    >
      <el-form-item label="手机号" prop="telephone" ref='telephone'>
        <el-input v-model="registerUser.telephone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="userPassword">
        <el-input  type="password"  v-model="registerUser.userPassword"></el-input>
      </el-form-item>
            <el-form-item label="确认密码" prop="userPassword2">
        <el-input type="password" v-model="registerUser.userPassword2"></el-input>
      </el-form-item>
       <el-form-item label="验证码" prop="vCode" ref="vCode">
        <el-input  v-model="registerUser.vCode" class="IsCode"></el-input>
            <el-button type="info" class="getCode" v-on:click="getvcode()">获取短信验证码</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "register",
  data:function(){
    //自定义验证表单元素的方法
     var validatePass2 = (rule, value, callback) => {
        if (value !== this.registerUser.userPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      registerUser:{
        telephone:'',
        userPassword:'',
        userPassword2:'',
        vCode:""
      },
      rules: {
          telephone: [
            {required:true,message:'手机号码不能为空', trigger: 'blur' },
            {min:11,max:11,message:'手机号码必须是11位',trigger:'blur'}
          ],
          userPassword: [
            {required:true,message:'密码不能为空', trigger: 'blur' },
            {min:6,max:6,message:'密码必须是6位',trigger:'blur'}
          ],
           userPassword2: [
            {required:true,message:'确认密码不能为空', trigger: 'blur' },
             { validator: validatePass2, trigger: 'blur' }
          ],
          vCode:[
            {required:true,message:"验证码不能为空",trigger:"blur"},
            {min:6,max:6,message:"密码是6位数",trigger:"blur"}
          ]
        }
    }
  },
  methods:{
    getvcode(){
      if(this.registerUser.telephone.length == 11){
        this.$axios.get('http://localhost:3000/register/getVCode',this.registerUser.telephone)
        .then(res => {
          console.log(res)
          this.sysvCode =res.data.data;
          alert("您的验证码为"+this.sysvCode);
        })
        .catch(err =>{
          console.log("错误信息:" + err)
        });
      }
    },
    submitForm(formName) {
      //通过ref定位到form表单
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log("系统验证码"+this.sysvCode)
            console.log("用户输入的验证码"+this.registerUser.vCode)
            if(this.registerUser.vCode == this.sysvCode){
            // alert('submit!');
            this.$axios.post('http://localhost:3000/register/register',this.registerUser)
            .then(res => {
              console.log('注册成功！',res)
              this.$router.push('/login') //路由转向登录组件
            })
            .catch(err =>{
              console.log(err);
            });
            }else{
              alert("您的验证码输入错误")
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        //实现重置表单元素数据
        this.$refs[formName].resetFields();
      }
  }
};
</script>
<style lang="css" scoped>
.register{
  width: 100%;
  height:100%;
  background: url(../assets/login1.jpg) no-repeat center center;
  background-size: 100% 100%;
}
.registerForm{
  width: 400px;
  height: 400px;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left:150px;
  margin-top: -280px;
  border-radius: 5px;
  background-color: rgba(245, 245, 245,0.7);
  padding: 20px 50px 10px 10px;
}
</style>